using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectDoiTac()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DOITAC
			};
			return View(CreateViewModel(data));
		}

        public ActionResult SelectDoiTacPaging(int page = 1)
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_DOITAC,
                CurrentPage = page
            };
            return PartialView("Templates/TH_ListDoiTac", CreateViewModel(data));
        }

		[AcceptVerbs(HttpVerbs.Delete)]
		public JsonResult DeleteDoiTac(int id)
		{
			var deleteItem = _repDoiTac.SelectByID(id);
			var fileName = deleteItem.HinhMinhHoa;
			var result = _repDoiTac.Delete(id);
			if (result)
			{
				if (fileName != "noimages.jpg")
				{
					var fullPath = Server.MapPath("/Content/Images/Items/" + fileName);
					DeleteFile(fullPath);
				}
			}
			return Json(new{
				Success = result,
				RecordCount = _repDoiTac.SelectAll().Count,
				DeleteId = id
			});
		}

		public ActionResult DetailOfDoiTac(int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_DOITAC,
				IntID = id
			};
			return View(CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateDoiTac(FormCollection forms)
		{
			string id = forms.GetValues("DoiTac_ID")[0];
			string tendoitac = forms.GetValues("DoiTac_TenDoiTac")[0];
			string diachi = forms.GetValues("DoiTac_DiaChi")[0];
			string dienthoai = forms.GetValues("DoiTac_DienThoai")[0];
			string website = forms.GetValues("DoiTac_Website")[0];
			string email = forms.GetValues("DoiTac_Email")[0];
			var file = Request.Files["DoiTac_HinhMinhHoa"];
			string refName = string.Empty;
			var editItem = _repDoiTac.SelectByID(int.Parse(id));
			editItem.TenDoiTac = tendoitac;
			editItem.DiaChi = diachi;
			editItem.DienThoai = dienthoai;
			editItem.Website = website;
			editItem.Email = email;
			var result = false;
			if (file.ContentLength != 0)
			{
				result = UploadFile(file, "/Content/Images/Items/", ref refName);
				editItem.HinhMinhHoa = refName;
			}
			result = _repDoiTac.Save();
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_DOITAC,
				IntID = int.Parse(id.Replace(".", string.Empty).Replace(",", string.Empty)),
				InfoText = result ? "Item has been updated" : "Cannot update this item"
			};
			return View("DetailOfDoiTac", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertDoiTac(FormCollection forms)
		{
			string tendoitac = forms.GetValues("DoiTac_TenDoiTac")[0];
			string diachi = forms.GetValues("DoiTac_DiaChi")[0];
			string dienthoai = forms.GetValues("DoiTac_DienThoai")[0];
			string website = forms.GetValues("DoiTac_Website")[0];
			string email = forms.GetValues("DoiTac_Email")[0];
			var file = Request.Files["DoiTac_HinhMinhHoa"];
			string refName = string.Empty;
			var newItem = new DoiTac
			{
				TenDoiTac = tendoitac,
				DiaChi = diachi,
				DienThoai = dienthoai,
				Website = website,
				Email = email,
				HinhMinhHoa = "noimages.jpg"
			};
			var result = _repDoiTac.Insert(newItem);
			if (result)
			{
				if (file.ContentLength != 0)
				{
					result = UploadFile(file, "/Content/Images/Items/", ref refName);
					if (result)
					{
						var addedItem = _repDoiTac.SelectByID(newItem.ID);
						addedItem.HinhMinhHoa = refName;
						_repDoiTac.Save();
					}
				}
			}
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DOITAC,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectDoiTac", CreateViewModel(data));
		}


	}
}